﻿package igz.fleaxy.ui.table 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormatAlign;
	import igz.fleaxy.pattern.Dto;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.Convert;
	import igz.fleaxy.util.DataTableUtil;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class TableFieldUi extends Sprite {
		protected const _TAGs : Array = [
											"DT" // Devuelve el valor del campo indicado, ejemplo: <DT>IdTabla@Tabla</DT>
										];

		protected var _RowUi : TableRowUi;
		protected var _LabelUi : LabelUi;

		protected var _Settings : Object;
		public function get $Settings() : Object {
			return _Settings;
		}
		public function set $Settings( p_settings:Object ) : void {
			ObjectUtil.$Merge( p_settings, _Settings );

			if ( p_settings.hasOwnProperty("styleName") ) {
				_LabelUi.$StyleName = _Settings["styleName"];
			}
			
			if ( p_settings.hasOwnProperty("maxLines") ) {
				_LabelUi.$SetMaxLines( _Settings["maxLines"] );
			}
		}
		
		private var _FieldWidth : Number;

		/**
		 * 
		 * @param	p_rowUi Referencia a su fila
		 * @param	p_fields
		 * @param	p_settings = { "styleName" : "styleRow0"
		 * 				, "fixWidth" : 100
		 * 				, "maxLines" : 1
		 * 				}
		 * @param	p_data
		 */
		public function TableFieldUi(p_rowUi:TableRowUi, p_settings:Object) {
			_Settings = {
				 "styleName" : ""
				,"maxLines" : 1
				,"textAlign" : TextFormatAlign.LEFT
			};
			ObjectUtil.$Merge( p_settings, _Settings );
			_RowUi 	= p_rowUi;

			_LabelUi = new LabelUi( ""
									 , _Settings["styleName"]
									 , { "maxLines": _Settings["maxLines"] }
									 );
			addChild(_LabelUi);

			if ( _Settings["sortField"] ) {
				buttonMode = true;
				useHandCursor = true;
				tabEnabled = false;
				_LabelUi.y = 5;
				addEventListener(MouseEvent.MOUSE_DOWN, _OnMouseDown);
			}
		}
		
		private function _OnMouseDown ( p_mouseEvent:MouseEvent ) : void {
			_RowUi.$TableUi.$Sort( this );
			
			if ( _RowUi.$TableUi.$Settings["onColumnSort"] is Function ) {
				_RowUi.$TableUi.$Settings["onColumnSort"]( this );
			}
		}
		
		public function $Load( p_dataTableRow:Array, p_inner:* ) : void {
			if ( p_inner is Function ) {
				addChild( p_inner( p_dataTableRow ) );
			}
			else if ( p_inner ) {
				_LabelUi.text = _ReplaceTags( p_dataTableRow, p_inner );
			}
		}
		
		public function $SetInner( p_inner:String ) : void {
			_LabelUi.text = p_inner;
		}
		
		private function _ReplaceTags( p_dataTableRow:Array, p_inner:String ) : String {
			var dataTable : Object = _RowUi.$TableUi.$DataTable;
			var tag : String;
			var tagMatch : String;
			var columnName : String;
			for each ( tag in _TAGs ) {
				for each ( tagMatch in p_inner.match( new RegExp("<" + tag + ">(.*?)</" + tag + ">", "gm") ) ) {
					columnName = tagMatch.replace("<" + tag + ">", "").replace("</" + tag + ">", "");
					p_inner = p_inner.replace(tagMatch, p_dataTableRow[ DataTableUtil.$ColumnIndex(dataTable,columnName) ]);
				}
			}
			
			if ( p_inner.indexOf("<ROWS />") != -1 ) {
				p_inner = p_inner.replace("<ROWS />", dataTable[DataTableUtil.$ROWS_LABEL].length);
			}
			
			return p_inner;
		}
		
		public function $Draw( p_width : uint ) : void {
			_FieldWidth = p_width;

			//_LabelUi.$SetFixWidth( p_width );
			scrollRect = new Rectangle(0, 0, _FieldWidth, _LabelUi.y + _LabelUi.height);
			switch ( _Settings["textAlign"] ) {
				case TextFormatAlign.CENTER:
					_LabelUi.x = _FieldWidth - _LabelUi.width >> 1;
				break;
				case TextFormatAlign.RIGHT:
					_LabelUi.x = _FieldWidth - _LabelUi.width;
				break;
			}
			
			graphics.clear();
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, _FieldWidth, height);
			graphics.endFill();
		}

		override public function get width() : Number {
			return _FieldWidth;
		}
		
		override public function get height() : Number {
			return _LabelUi.y + _LabelUi.height;
		}
	}
	
}